<?php
/**
 * Created by PhpStorm.
 * User: 17274
 * Date: 2018/3/27
 * Time: 21:06
 */
namespace app\admin\model;

use think\Model;
use think\Validate;
use think\Config;

class AlbumModel extends  Model
{
    protected $table = 'yui_album';
    /*
     * rule
     * 验证data
     * @params $data
     * 新增和修改时验证
     * */
    public function validate_model($scene,$data){
        $rule = [
            'tag' => 'chsDash',
            'label' => 'alpha',
            'status' => 'number'
        ];
        $msg = [
            'tag.chsDash' => '标注不合法!',
            'label.alpha' => '标签code必须为字母!',
            'status.number' => '标签状态必须为数字类型!',
        ];
        $validate = new Validate($rule, $msg);
        $result = $validate->check($data);
        if(!$result){
            return [
                'code'=>0,
                'text'=> $validate->getError()
            ];
        }
        return ['code'=>1];
    }

    /*
   * 根据ID获取信息
   * */
    public function getById($id){
        return $this->where(["id"=>$id])->find();
    }

    /*
    * 根据MAP获取信息
    * */
    public function getAlbumThumb($code){
        $map=[
            "label" => $code,
            "status" => 1
        ];
        $result=[
            'data' => $this->where($map)->order('updated_at desc')->find(),
            'count' => $this->where($map)->count()
        ];
        return $result;
    }

    /*
     * 列表
     * */
    public function listAll(){
       return $this->order('updated_at desc')->select();
    }

    /*
     * 根据label列表
     * */
    public function listAllByLabel($label){
        return $this->where('label',$label)->order('updated_at desc')
            ->paginate(Config::get('page_info')['album'],false,[
            'type'     => 'bootstrap',
            'query' => [
                'code' => $label
            ]
        ]);
    }

    /*
     * 根据label status=1列表
     * */
    public function listAllStatusByLabel($label){
        return $this->where(['label'=>$label,'status'=>1])->order('updated_at desc')
            ->paginate(Config::get('page_info')['page_album_index'],false,[
                'type'     => 'bootstrap',
                'query' => [
                    'label' => $label
                ]
            ]);
    }

    /*
     * 启用禁用列表
     * */
    public function listOnOff($status){
        $data=$this->where('status',$status)->order('updated_at desc')->select();
        return $data;
    }

    /*
     * 新增标签
     * */
    public function add($datas){
        if($this->saveAll($datas)){
            $name='';
            foreach ($datas as $data){
                $name.=$data['name'].'#';
            }
            $params=[
                Config::get('op_type')[0],
                Config::get('op_module')[5],
                Config::get('op_text')[6],
                $name
            ];
            tag('logs_db',$params);
            return op_success();
        }
        return op_fail();
    }

    /*
     * 修改标签
     * */
    public function edit($data){
        $data['updated_at']=time();
        unset($data['__token__']);
        $id=input('post.id/d');
        $old_album=$this->getById($id)->toArray();
        $data['name']=$old_album['name'];
        //unlink old_file
        unlink($old_album['path']);
        if($this->where('id',$id)->update($data)){
            $params=[
                Config::get('op_type')[1],
                Config::get('op_module')[5],
                Config::get('op_text')[6],
                $data['name']
            ];
            tag('logs_db',$params);
            return op_success();
        }
        return op_fail();
    }

    /*
     * 删除
     * */
    public function del(){
        $ids=input('post.ids/a');
        $op_name=[];
        foreach ($ids as $id){
            $re=$this->getById($id)->toArray();
            array_push($op_name,$re['name']);
            unlink($re['path']);
        }
        if ($this->destroy($ids)){
            $params=[
                Config::get('op_type')[2],
                Config::get('op_module')[5],
                Config::get('op_text')[6],
                implode(",", $op_name)
            ];
            tag('logs_db',$params);
            return op_success();
        }
        return op_fail();
    }

}